<?php
class MY_Controller extends CI_Controller
{
    public function __construct(){      
	   parent::__construct();	   
	   header("Content-type: text/html; charset=utf-8");
	   
//	   $this->settings->load('config');
//	   $this->webconf = setting('config');
    }	
}

class Admin_Controller extends CI_Controller
{
    public function __construct(){      
        parent::__construct();
        header("Content-type: text/html; charset=utf-8");
	if (!$this->session->userdata('username')){
            $redirect = $this->uri->uri_string();
            if ( $_SERVER['QUERY_STRING']){
                $redirect .= '?' . $_SERVER['QUERY_STRING'];
            }
            redirect(site_url('login').'?redirect='.urlencode(base64_encode($redirect)));
        }    
        $this->load->driver('cache');//导入缓存驱动类
        $this->memcached = $this->cache->memcached;//全局memcached句柄
        //加载提现奖励配置
        $this->settings->load('award');
        $this->config_award = setting('award');
//        p($this->config_award);die;
        //通知模板
        $this->settings->load('notice_tpl');
        $this->config_tpl = setting('notice_tpl');
        
        //加载任务配置
        $this->settings->load('task');
        $this->config_task = setting('task');
        
        //加载平台背景类别
        $this->settings->load('bg_type');
        $this->bgType = setting("bg_type");
        //社区文章类别板块
        $this->settings->load('forummenu');
        $this->forummenu = setting("forummenu");
        $this->load->model('admin_model');
        $this->load->model('admin_group_model');
        $this->load->model('menu_model');
        $this->settings->load('menus');
        $this->settings->load('config');
        $this->webconf = setting('config');
//        $conf_site = setting('site');
//        define("SITE_PROTOCOL",$conf_site['site_domain']);
//        $this->template->assign('site_protocol',SITE_PROTOCOL);
        //获取用户名
        $username = $this->session->userdata('username');
        //用户信息
        $this->userinfo = $this->admin_model->Get($username);
        $this->template->assign('info',$this->userinfo);       
        $this->template->assign('username',$username);
        //用户所在组
        $id=$this->session->userdata('group_id');
        $menu_data=$this->admin_group_model->Get($id);
        $menu_data_str=$menu_data['access'];//当前用户的访问权限
        $menu_data_arr=explode(',', $menu_data_str);
        foreach ($menu_data_arr as $key=>$v){
                $menu_data_arr[$key]=explode('_', $v);;
        }
//        p($menu_data_arr);die;
        $access_arr=array();

        foreach ($menu_data_arr as $val){
                $access_arr[$val[0]][]=$val[1];
        }
        //获取配置文件中的所以菜单
        $this->menu = setting('menus');
        $this->current = $this->uri->segment(2)? $this->uri->segment(2) : 'index';
//        echo $this->current;die;
        $segment_t = $this->uri->segment(1);
        $access=$segment_t."_".$this->current;
        $access_msg_arr=explode(',', $menu_data_str);
        $on=false;

        if (in_array($access, $access_msg_arr)) {
                $on=true;
        }
        foreach ($access_msg_arr as $v){
                if(strstr($access,$v) || $access=='index_index' || $access=="member_password"){
                        $on=true;
                }
        }
        $allAccess=$this->menu_model->GetAll();
        
        foreach ($allAccess as $key => $val){
                $allAccess[$key]=$val['group'].'_'.$val['url'];
        }
        if(!in_array($access, $allAccess)){
                $on=true;
        }

        if(!$on){
                echo "没有权限";
                die;
        }

        $segment_c = $this->uri->segment(3)? $this->uri->segment(3) : 'index';
        
        $segment_p = $this->uri->segment(2);
        if($segment_c){
                $segment_p = $segment_p.'/'.$segment_c;
        }else{
                $segment_p = $segment_p;
        }
        $this->template->assign('segment_t',$segment_t);
        $this->template->assign('segment_p',$this->current);
        $pagekey = $segment_t.'/'.$segment_p;
        
            $pagetitle = $this->menu_model->GetName($pagekey);
        $this->template->assign('pagetitle',$pagetitle);
        
            $this->topmenu = $this->menu[1]['children'];
            $this->premeun = $this->topmenu;
        foreach ($this->topmenu as $key=>$val){
                    if(!in_array($val[url], array_keys($access_arr))){
                            unset($this->topmenu[$key]);
                    }else{
                            $arr=array();
                            if($val['children']){
                                 foreach ($val['children'] as $k=>$v){
                                    if(in_array($v[url], $access_arr[$val[url]])){
                                            $arr[$k]=$v;
                                    }
                                }
                            }
                            $this->topmenu[$key]['children']=$arr;
                    }
            }
            
            
       $this->menu_id = $this->menu_model->findkey($this->topmenu,$segment_t);
       $this->template->assign('menu_id',$this->menu_id);
       $this->template->assign('premeun',$this->premeun);
       $this->template->assign('topmenu',$this->topmenu);
       $this->leftmenu = $this->topmenu[$this->menu_id];
       $this->template->assign('leftmenu',$this->leftmenu);
    }	
    
    //缓存热门文章 12篇
    public function cache_hot_article(){
        $where = "a.hot = 1 and a.status = 1 and a.show = 1 order by a.sort desc";
        $query_params = "a.id,a.authorid,a.title,a.summary,a.cat_id,a.thumb,a.tags_name,a.status,a.create_time,c.name cat_name,u.nickname";
        $sqlData = "select $query_params from tan_bbs_article a left join tan_bbs_article_category c on c.id = a.cat_id left join tan_users_info u on u.user_id = a.authorid where ".$where." limit 12";//构造查询结果记录
        $list = $this->db->query($sqlData)->result_array();
       //缓存热门文章
       $this->memcached->save("www_hot_article",serialize($list),86400);//缓存1天
    }
    
}

class Admins_Controller extends CI_Controller
{
    public function __construct(){
		parent::__construct();
	   
		header("Content-type: text/html; charset=utf-8");
		$this->load->model('user_model');
		$this->load->model('admin_group_model'); 
    }
}

